package com.atjava.nebula.core.util.tree;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import com.atjava.nebula.core.exception.baseExp.SystemException;
import com.atjava.nebula.core.model.TreeModel;

public class TreeUtils {
	
	
	private static List<TreeModel> getTreeObjectList(Map<Object,TreeModel> treeNodeMap){
		List<TreeModel> treeNodes = new ArrayList<TreeModel>();
		for (Iterator<Object> iterator = treeNodeMap.keySet().iterator(); iterator.hasNext();) {
			Object key = iterator.next();
			TreeModel node = treeNodeMap.get(key);
			if(node.getParentPkid() != null){
				TreeModel pNode = treeNodeMap.get(node.getParentPkid());
				if(pNode != null){
					if(pNode.getChildNodes() == null)
						pNode.setChildNodes(new ArrayList<TreeModel>());
					if(!pNode.getChildNodes().contains(node)){
						pNode.getChildNodes().add(node);
					}
				}
				
			}else{
				if(!treeNodes.contains(node)){
					treeNodes.add(node);
				}
			}
		}
		return treeNodes;
	}
	
	public static List<TreeModel> getTreeObjectList(List<TreeModel> treeNodeList){
		Map<Object,TreeModel> treeNodeMap = new HashMap<Object, TreeModel>();
		for (int i = 0; i < treeNodeList.size(); i++) {
			TreeModel tModel = treeNodeList.get(i);
			treeNodeMap.put(tModel.getPkid() , tModel);
		}
		return getTreeObjectList(treeNodeMap);
	}
	
	private static TreeModel getTreeObject(Map<Object,TreeModel> treeNodeMap,TreeModel treeNode){
		TreeModel tModel = null;
		for (Iterator<Object> iterator = treeNodeMap.keySet().iterator(); iterator.hasNext();) {
			Object key = iterator.next();
			TreeModel node = treeNodeMap.get(key);
			if(treeNode.getPkid().equals(node.getPkid())){
				tModel = node;
			}
			if(node.getPkid() != null){
				TreeModel pNode = treeNodeMap.get(node.getParentPkid());
				if(pNode != null){
					if(pNode.getChildNodes() == null)
						pNode.setChildNodes(new ArrayList<TreeModel>());
					if(!pNode.getChildNodes().contains(node)){
						pNode.getChildNodes().add(node);
					}
				}
			}
		}
		return tModel;
	}
	
	public static TreeModel getTreeObject(List<TreeModel> treeNodeList,TreeModel treeNode){
		Map<Object,TreeModel> treeNodeMap = new HashMap<Object, TreeModel>();
		for (int i = 0; i < treeNodeList.size(); i++) {
			TreeModel tModel = treeNodeList.get(i);
			treeNodeMap.put(tModel.getPkid() , tModel);
		}
		return getTreeObject(treeNodeMap,treeNode);
	}
	
	public static List<TreeModel> sortTreeList(List<TreeModel> treeNodeList){
		
		List<TreeModel> sortedTreeList = new ArrayList<TreeModel>();
		List<TreeModel> objectTreeList = getTreeObjectList(treeNodeList);
		
		for (int i = 0; i < objectTreeList.size(); i++) {
			addToTreeSortList(sortedTreeList, objectTreeList.get(i));
		}
		return sortedTreeList;
	}
	
	private static void addToTreeSortList
		(List<TreeModel> treeNodesList,TreeModel treeObject){
		treeNodesList.add(treeObject);
		List<TreeModel> childNodesList = treeObject.getChildNodes();
		if(childNodesList != null){
			for (int i = 0; i < childNodesList.size(); i++) {
				TreeModel childNode = childNodesList.get(i);
				addToTreeSortList(treeNodesList,childNode);
			}
		}
	}
	
	public static boolean isTreeModel(Class<?> entityClass){
		try {
			return (entityClass.newInstance() instanceof TreeModel);
		} catch (Exception e) {
			throw new SystemException(e,"Creating instance [" + entityClass +"]");
		}
	}
	
	
}
